import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useRouteHistoryStore = defineStore('RouteHistory', () => {
  const historySet = ref([])
  function historyPushback(route) {
    historySet.value.push(route)
  }

  function uniqueValidate(path) {
    const pathSplit = path.split('/')
    const name = pathSplit[pathSplit.length - 1]
    const result = historySet.value.find((v) => v.name === name)
    if (result) {
      return false
    }

    return true
  }

  function historyRemove(keyPath) {
    if (historySet.value.length === 1) return
    historySet.value = historySet.value.filter((v) => v.fullPath !== keyPath)
  }

  return { historySet, uniqueValidate, historyPushback, historyRemove }
})
